<?php

namespace App\Http\Controllers\Api;

use App\Exceptions\BusinessException;
use App\Http\Resources\GoodsModelResource;
use App\Models\FavModel;
use App\Models\GoodsPicPrimaryModel;
use App\Utils\ControllerModelTrait;
use Carbon\Carbon;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class FavController extends ApiController
{
    use ControllerModelTrait;

    /**
     * 获取用户收藏列表
     * @param Request $request
     * @return JsonResponse
     * @throws BusinessException
     */
    public function getList(Request $request): JsonResponse
    {
        $uuid = $this->verifyString('uuid');
        $paginate = FavModel::where('uuid', $uuid)->select(['PID'])->orderByDesc('CTime')->paginate(20);
        $list = $this->getPageGoods($paginate);
        return $this->success($this->getPaginate($paginate, GoodsModelResource::collection($list)));
    }

    /**
     * 添加/取消收藏
     * @param Request $request
     * @return JsonResponse
     * @throws BusinessException
     */
    public function doSave(Request $request): JsonResponse
    {
        $uuid = $this->verifyString('uuid');
        $pid = $this->verifyInteger('pid');
        if ($fav = FavModel::where('uuid', $uuid)->where('PID', $pid)->first())
            $fav->delete();
        else
            FavModel::create(['uuid' => $uuid, 'PID' => $pid, 'CTime' => Carbon::now()->toDateTimeString()]);
        return $this->success();
    }

    /**
     * 删除（失效）收藏
     * @param Request $request
     * @return JsonResponse
     */
    public function doDelete(Request $request): JsonResponse
    {
        if ($request->input('uuid') && $request->input('pid'))
            FavModel::where('uuid', $request->input('uuid'))->whereIn('PID', explode(',', $request->input('pid')))->delete();
        return $this->success();
    }

    /**
     * 收藏产品下架通知
     * @param Request $request
     * @return JsonResponse
     * @throws BusinessException
     */
    public function noticeList(Request $request): JsonResponse
    {
        $uuid = $this->verifyString('uuid');
        $pidArray = FavModel::whereHas('goods', function ($query) {
            $query->where('PStatus', 3);
        })->where('uuid', $uuid)->orderByDesc('ID')->pluck('PID');
        $paginate = GoodsPicPrimaryModel::whereIn('ID', $pidArray)->paginate(20);
        return $this->success($this->getPaginate($paginate, GoodsModelResource::collection($paginate->items())));
    }

    /**
     * 根据pid获取图片商品列表
     * @param $paginate
     * @return array
     */
    private function getPageGoods($paginate): array
    {
        $pidArray = [];
        foreach ($paginate as $item) {
            $pidArray[] = $item->PID;
        }
        $collection = GoodsPicPrimaryModel::whereIn('ID', $pidArray)->where('PID', 0)
            ->orderByDesc('ID')->get();
        // 原顺序排序
        $data = [];
        foreach ($collection as $v) {
            $data[$v->ID] = $v;
        }
        $list = [];
        foreach ($pidArray as $pid){
            $list[] = $data[$pid];
        }
        return $list;
    }
}
